package com.example.spring.controller;


import com.example.spring.entity.*;
import com.example.spring.entity.Record;
import com.example.spring.mapper.GoodMapper;
import com.example.spring.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;

@RestController
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private GoodService goodService;

    //新增或修改
    @PostMapping()
    public Integer save(@RequestBody Good good) {
        //新增或者更新
        return goodService.save(good);
    }
    //查询所有数据
    @GetMapping()
    public List<Good> findAll() {
        List<Good> all =  goodMapper.findAll();
        return all;
    }
    //删除
    @DeleteMapping("/{gid}")
    public Integer delete(@PathVariable Integer gid)
    {
        return goodMapper.deleteById(gid);
    }

    //按type统计某指定仓库的各种商品的数量
    @PostMapping("/select")
    public List<Object> find_by_whid_type(@RequestParam Integer whid){
        List<Integer> total = goodMapper.findByWhidType(whid);
        List<String> type = goodMapper.findWhType(whid);
        List<Object> res = new ArrayList<Object>();
        for (int i = 0; i < total.size(); i++)
        {
            Map<String, Object> res1 = new HashMap<>();
            res1.put("type", type.get(i));
            res1.put("total", total.get(i));
            res.add(res1);
        }
        return res;
    }
    //分页查询指定仓库的指定类型商品的所有信息
    @PostMapping("/select/type/Page")
    public Map<String, Object> find_by_type_page(@RequestParam Integer whid,
                                                 @RequestParam String type,
                                                 @RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        List<Good> data =  goodMapper.findByType_page(whid, type, pageNum, pageSize);
        Integer total = goodMapper.findByType_Total(whid, type);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @PostMapping("/read/slot")
    public void readSlot(@RequestParam Integer whid,
                        @RequestBody Map<String, List<GS>> data0)
    {
        List<GS> data = data0.get("data");
        Iterator<GS> itr = data.iterator();
        GS tmp;
        while(itr.hasNext()) {
            tmp = itr.next();
            Integer gid = tmp.getGid();
            Integer sid = tmp.getSid();
            goodService.readSlot(gid, sid, whid);
        }
    }


}
